package dal

import (
	"database/sql/driver"
	"time"
)

const (
	// timeFormat = "2006-01-02 15:04:05"
	timeFormat = time.RFC3339
)

// Time .
type Time time.Time

func (t Time) String() string {
	return time.Time(t).Format(timeFormat)
}

// MarshalJSON .
func (t Time) MarshalJSON() ([]byte, error) {
	b := make([]byte, 0, len(timeFormat)+2)
	b = append(b, '"')
	b = time.Time(t).AppendFormat(b, timeFormat)
	b = append(b, '"')
	return b, nil
}

// Value insert timestamp into mysql need this function.
func (t Time) Value() (driver.Value, error) {
	tm := time.Time(t)
	if tm.IsZero() {
		return nil, nil
	}
	return tm, nil
}
